<template>
  <div class="answer">
    <div class="top">
      <div class="left-time">{{countdown}}s</div>
    </div>
    <div class="main">
      <div v-for="(item,key,index) in questionData" :key="index">
        <div v-if="currentSeq == key + 1" :class="titleBg[item.typeCode]">
          <div class="seq-area">
            <span>{{currentSeq}}/{{questionData.length}}</span>
          </div>
        </div>
        <!-- <div v-if="key + 1 == currentSeq" class="seq-area">
          <span>{{key + 1}}//{{questionData.length}}</span>
        </div> -->
        <div v-if="key + 1 == currentSeq" class="main-content">
          <div class="question-title">
            {{item.queContent}}
          </div>
          <div @click="selectFn(item,seq)" class="options" v-for="(answer,seq,i) in item.optionsMap" :key="i">
            <div v-if="!currentSubmitFlag" class="option" :class="item.selectSeq && item.selectSeq.includes(seq)?'selected':''">
              <div class="left">
                <div class="seq">{{seq | numberToLetter}}</div>
              </div>
              <div class="right">{{answer}}</div>
            </div>
            <div v-else class="option" :class="compareFn(item, seq)">
              <div class="left">
                <div class="seq">{{seq | numberToLetter}}</div>
              </div>
              <div class="right">
                <span> {{answer}}</span>
              </div>
              <img v-if="compareFn(item, seq) == 'yes'" class="result-img" src="../assets/image/right.png" alt="" />
              <img v-if="compareFn(item, seq) == 'no'" class="result-img" src="../assets/image/wrong.png" alt="" />
            </div>
          </div>
          <div v-if="currentSubmitFlag" class="analysis">
            <span style="color: black;font-size:16px;">答案：{{item.rightCode | numberToLetter}}</span><span style="margin-left: 30px;" @click="showAnalysis(item)" class="text">查看解析</span>
          </div>
        </div>
      </div>
    </div>
    <div v-if="!currentSubmitFlag" @click="submitFn()" :class="currentSelectFlag || allFinishFlag?'form-btn':'disable-btn'">
      <span>提交</span>
    </div>
    <div v-if="currentSubmitFlag&&!allFinishFlag" @click="nextFn()" class="form-btn">
      <span>下一题</span>
    </div>
    <div v-if="currentSubmitFlag&&allFinishFlag" @click="showResult()" class="form-btn">
      <span>查看结果</span>
    </div>
    <van-dialog v-model="analysisVisible" :lock-scroll="true" :show-confirm-button="false">
      <div class="frame-area-analysis">
        <!-- <img @click="analysisVisible = false" class="close-img" src="../assets/image/close.png" alt="" /> -->
        <div @click="analysisVisible = false" class="close-img"></div>
        <div class="frame-content">
          <div class="right-answer">
            <span>正确答案：</span>
            <span>{{currentRightAnswer | numberToLetter}}</span>
          </div>
          <div class="under-line">
          </div>
          {{analysisValue}}
        </div>
      </div>
    </van-dialog>
    <van-dialog v-model="resultVisible" :lock-scroll="true" :show-confirm-button="false">
      <div class="frame-area-result">
        <!-- <img @click="resultVisible = false" class="close-img" src="../assets/image/close.png" alt="" /> -->
        <div @click="resultVisible = false" class="close-img"></div>
        <div class="frame-content">
          <div class="result-title">
            <span>用时{{timeAmountFormat}}，共答对{{rightNum}}道题</span>
          </div>
          <div class="detail">
            <span v-if="!allRightFlag">只要您将所有题目都答对，即可获得一份电子证明，以表彰您的出色表现。请继续努力，挑战自我，争取这份荣誉吧！</span>
            <span v-else>恭喜您！您已经成功答对了所有题目，特此授予您一份电子证明。请立即前往领取，这是对您卓越表现的最佳肯定！</span>
          </div>
          <div class="result-btn">
            <span v-if="!allRightFlag" @click="continueFn()">继续挑战</span>
            <span v-else @click="goCert()">
              {{userInfo.certificateNo ? '查看证书' : '领取证书'}}
            </span>
          </div>
        </div>
      </div>
    </van-dialog>
  </div>
</template>
<script>
import { GetLists, PassCheck } from '../api/newapi';
export default {
  data () {
    return {
      analysisVisible: false, /*查看解析显隐*/
      resultVisible: false, /*查看结果*/
      analysisValue: '',/*当前解析内容*/
      currentSubmitFlag: false, /*当前题目是否提交答案*/
      currentSeq: 1, /*当前是第几道题目*/
      currentRightAnswer: '',/*当前题目正确答案*/
      rightNum: 0, /*答对题目数*/
      currentSelectFlag: false, /*当前题目是否选中了选项*/
      allFinishFlag: false, /*是否完成所有答题*/
      allRightFlag: false, /*是否全部正确*/
      countLimit: 30,
      countdown: 30,
      timeAmount: 0,
      timeAmountFormat: '',
      timer: null,
      userInfo: JSON.parse(localStorage.getItem('userInfo')) || {},
      questionData: [] /*10道题目详细数据*/,
      selectData: [], /*当前选中数据*/
      titleBg: { 6001: 'top-area1', 6002: 'top-area2', 6003: 'top-area3' }
    }
  },
  mounted () {
    this.initData()
  },
  methods: {
    initData () {
      clearInterval(this.timer)
      this.currentSubmitFlag = false
      this.currentSeq = 1
      this.rightNum = 0
      this.currentSelectFlag = false
      this.currentRightAnswer = ''
      this.allFinishFlag = false
      this.allRightFlag = false
      this.timeAmount = 0
      this.timeAmountFormat = ''
      this.questionData = []
      this.countdown = this.countLimit
      this.$toast.loading({
        message: '请稍后',
        duration: 0,
        forbidClick: true,
      });
      GetLists({ userId: this.userInfo.id }).then((res) => {
        this.$toast.clear()
        res.forEach(e => {
          e.selectSeq = []
        })
        this.questionData = res
        this.setTimerFn()
      })
    },
    setTimerFn () {
      this.timer = setInterval(() => {
        if (!this.countdown) {
          clearInterval(this.timer)
          this.submitFn('auto')
          return
        }
        this.countdown--
      }, 1000);
    },
    selectFn (item, seq) {
      this.currentSelectFlag = true
      if (this.currentSubmitFlag) {
        return
      }
      if (item.typeCode == 6002) {
        const index = this.selectData.indexOf(seq);
        if (index !== -1) {
          // 如果seq已存在,则移除
          this.selectData.splice(index, 1);
        } else {
          // 如果seq不存在,则添加
          this.selectData.push(seq);
        }
      } else {
        this.selectData = [seq]
      }
      this.$set(item, 'selectSeq', this.selectData)
    },
    showAnalysis (v) {
      this.analysisVisible = true
      this.analysisValue = v.analysis
      this.currentRightAnswer = v.rightCode
    },
    submitFn (v) {
      if (this.currentSeq == this.questionData.length) {
        this.allFinishFlag = true
      }
      if (!v && !this.currentSelectFlag) {
        return
      }
      this.timeAmount += (this.countLimit - this.countdown)
      clearInterval(this.timer)
      this.currentSubmitFlag = true
    },
    nextFn () {
      this.currentSelectFlag = false
      this.currentSubmitFlag = false
      this.currentSeq++
      this.countdown = this.countLimit
      this.selectData = []
      this.setTimerFn()
    },
    showResult () {
      console.log(this.timeAmount, 'this.timeAmountthis.timeAmount')
      this.rightNum = 0
      this.questionData.forEach(e => {
        if (e.selectSeq.sort().join('') == e.rightCode) {
          this.rightNum++
        }
      })
      if (this.rightNum == this.questionData.length) {
        this.allRightFlag = true
      } else {
        this.allRightFlag = false
      }
      this.resultVisible = true
      this.timeAmountFormat = this.convertSeconds(this.timeAmount)
    },
    goCert () {
      if(!this.userInfo.certificateNo){
        PassCheck({ id: this.userInfo.id }).then((res) => {
          this.userInfo.pass = 'pass'
          this.userInfo.certificateNo = res.certificateNo
          localStorage.setItem('userInfo', JSON.stringify(this.userInfo))
        })
      }
      this.$router.replace("/cert");
    },
    continueFn () {
      this.resultVisible = false
      this.selectData = []
      this.initData()
    },
    convertSeconds (seconds) {
      const minutes = Math.floor(seconds / 60);
      const remainingSeconds = seconds % 60;
      const formattedSeconds = remainingSeconds < 10 ? '0' + remainingSeconds : remainingSeconds;
      return `${minutes}分${formattedSeconds}秒`;
    },
    compareFn (item, seq) {
      const inRight = item.rightCode.toString().indexOf(seq) != -1
      const inSelect = item.selectSeq.includes(seq)
      if (item.typeCode == 6002) {
        return inRight && inSelect ? 'yes' : (inSelect && !inRight || !inSelect && inRight) ? 'no' : ''
      } else {
        return inRight ? 'yes' : inSelect ? 'no' : ''
      }
      // return +item.rightCode.indexOf(seq) != -1 ? 'yes' : item.selectSeq.includes(seq) ? 'no' : ''
    }
  },
  filters: {
    numberToLetter (value) {
      const map = { '1': 'A', '2': 'B', '3': 'C', '4': 'D' };
      return value.toString().split('').map(num => map[num] || num).join('');
    }
  }
}
</script>
<style lang="scss" scoped>
.answer {
  position: relative;
  width: 100%;
  height: 100%;
  padding-bottom: 53px;
  background: url('../assets/image/answer1.png');
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
  .top {
    margin: 0 auto;
    text-align: center;
    height: 100px;
    background: url('../assets/image/time_circle.png');
    background-size: 80px 80px;
    background-position: center;
    background-repeat: no-repeat;
    .left-time {
      font-family: DIN;
      font-weight: bold;
      font-size: 22px;
      color: #0099ff;
      position: relative;
      top: 36px;
    }
    .answer-top-img {
      width: 80px;
    }
  }
  .main {
    position: relative;
    // padding: 0 40px;
    padding: 0;
    // height: 486px;
    .top-area1 {
      height: 80px;
      background: url('../assets/image/answer_type1.png');
      background-size: 90% 100%;
      background-position: center;
      background-repeat: no-repeat;
    }
    .top-area2 {
      height: 80px;
      background: url('../assets/image/answer_type2.png');
      background-size: 90% 100%;
      background-position: center;
      background-repeat: no-repeat;
    }
    .top-area3 {
      height: 80px;
      background: url('../assets/image/answer_type3.png');
      background-size: 90% 100%;
      background-position: center;
      background-repeat: no-repeat;
    }
    .seq-area {
      width: 36px;
      height: 18px;
      font-family: Source Han Sans CN;
      font-weight: bold;
      font-size: 18px;
      color: #ffffff;
      position: relative;
      top: 52px;
      left: 46px;
    }
    .main-content {
      padding: 20px 40px 40px;
      background: url('../assets/image/answer_line.png');
      background-size: 90% 100%;
      background-position: center;
      background-repeat: no-repeat;
    }
    .question-title {
      font-family: Source Han Sans SC;
      font-weight: bold;
      font-size: 17px;
      color: #333333;
      line-height: 29px;
      margin-bottom: 32px;
    }
    .options {
      .option,
      .selected {
        display: flex;
        align-items: center;
        padding: 12px 16px;
        background: #f5f5f5;
        border: 1px solid #f5f5f5;
        border-radius: 10px;
        margin-bottom: 24px;
        .left {
          width: 40px;
          font-family: Source Han Sans CN;
          font-weight: 400;
          font-size: 16px;
          .seq {
            background: #fff;
            width: 25px;
            height: 25px;
            line-height: 25px;
            border-radius: 100%;
            text-align: center;
          }
        }
        .right {
          font-family: Source Han Sans CN;
          font-weight: 400;
          font-size: 15px;
          color: #333333;
          line-height: 19px;
        }
      }
      .option {
        position: relative;
        .result-img {
          position: absolute;
          top: -10px;
          right: -10px;
          width: 26px;
        }
      }
      .selected {
        border-radius: 10px;
        border: 1px solid #0699e9;
        .left {
          .seq {
            background: #0699e9;
            color: #fff;
          }
        }
      }
      .yes {
        border-radius: 10px;
        background: #0699e9;
        border: 1px solid #0699e9;
        .left {
          .seq {
            color: #0699e9;
          }
        }
        .right {
          color: #fff;
        }
      }
      .no {
        border-radius: 10px;
        background: #fc8a04;
        border: 1px solid #fc8a04;
        .left {
          .seq {
            color: #fc8a04;
          }
        }
        .right {
          color: #fff;
        }
      }
    }
    .analysis {
      width: 100%;
      text-align: center;
      font-family: Source Han Sans CN;
      font-weight: 500;
      font-size: 19px;
      color: #0699e9;
      padding-bottom: 10px;
      .text {
        border-bottom: 1px solid #0699e9;
      }
    }
  }
  .form-btn,
  .disable-btn {
    margin: 44px auto;
    width: 240px;
    height: 46px;
    line-height: 46px;
    text-align: center;
    background: linear-gradient(
      0deg,
      rgba(1, 115, 211, 0.97),
      rgba(83, 198, 254, 0.97)
    );
    border-radius: 23px;
    font-family: Source Han Sans CN;
    font-weight: 500;
    font-size: 19px;
    color: #ffffff;
    border: 1px solid transparent;
  }
  .disable-btn {
    background: transparent;
    border: 1px solid #0699e9;
    color: #0699e9;
  }
  .frame-area-analysis,
  .frame-area-result {
    position: relative;
    width: 100%;
    height: 480px;
    .frame-img {
      width: 100%;
    }
    .frame-content {
      padding: 0 27px 200px;
      font-family: Source Han Sans CN;
      font-weight: 400;
      font-size: 15px;
      color: #333333;
      line-height: 23px;
      height: 360px;
      overflow-y: auto;
      position: relative;
      top: 54px;
      .detail {
        font-family: Source Han Sans CN;
        font-weight: 400;
        font-size: 15px;
        color: #333333;
        line-height: 26px;
        padding-top: 18px;
        text-indent: 2em;
      }
      .right-answer {
        font-family: Source Han Sans CN;
        font-weight: bold;
        font-size: 16px;
        color: #0699e9;
        line-height: 18px;
      }
      .under-line {
        width: 125px;
        height: 9px;
        background: #0699e9;
        border-radius: 5px;
        opacity: 0.1;
        position: relative;
        top: -6px;
        left: -6px;
      }
    }
    .close-img {
      width: 34px;
      height: 34px;
      position: absolute;
      top: 0px;
      right: 0px;
    }
    .result-btn {
      margin: 20px auto 0;
      text-align: center;
      width: 154px;
      height: 39px;
      line-height: 39px;
      background: #0699e9;
      border-radius: 20px;
      font-family: Source Han Sans CN;
      font-weight: 400;
      font-size: 17px;
      color: #ffffff;
    }
    .result-title {
      margin: 0 auto;
      text-align: center;
      width: 263px;
      height: 47px;
      line-height: 47px;
      border-radius: 23px;
      font-family: Source Han Sans CN;
      font-weight: 500;
      font-size: 15px;
      color: #0699e9;
      background: url('../assets/image/answer3.png');
      background-size: 100% 100%;
      background-position: center;
      background-repeat: no-repeat;
    }
  }
  .frame-area-analysis {
    background: url('../assets/image/analysis_dialog.png');
    background-size: 100% 100%;
    background-position: center;
    background-repeat: no-repeat;
  }
  .frame-area-result {
    background: url('../assets/image/result_dialog.png');
    background-size: 100% 100%;
    background-position: center;
    background-repeat: no-repeat;
    height: 280px;
    .frame-content {
      height: 280px;
      top: 40px;
      left: -6px;
    }
  }
}
</style>
<style lang="scss" >
.answer {
  .van-dialog {
    top: 50%;
    left: 52%;
    background-color: transparent;
  }
}
</style>